#include <iostream>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <stdlib.h>
#include <regex>
#include <string>
#include <cstring>
#include <unistd.h>
#include <typeinfo>
#include <sstream>
#include <chrono>
#include <thread>
#include "filestream.cpp"
#include "split.cpp"
// #include "sqlite3.cpp"

// g++ -o time time.cpp -lpthread

using namespace std;
using json=nlohmann::json;

void timestart(std::string date);
void callback();

int status=1;


void callback()
{
    const char* path    ="time.json";

    string times        =file_get_contents(path);
    json timej          =json::parse(times);
    timej["date"][0]    ="0000";
    timej["status"]     =0;
    for (int i = 1; i < 6; i++) timej["date"][i]="00";
    file_put_contents(path,timej.dump()); 
}

void timestart(std::string date)
{
    // std::getline(cin,date);
    const char* path="time.json";
    struct tm timeinfo                ={};
    std::istringstream                ss(date);
    ss >>                             std::get_time(&timeinfo,"%Y-%m-%d %H:%M:%S");
    long timestamp                    =(long)std::mktime(&timeinfo);
    while(1)
    {
        long ptime                        =((timestamp-(long)time(0))-28800)-1;
        if(ptime+28800<=0) 
        {
            status=0;
            break;
        }
        else
        {
            std::tm local_time                =*std::localtime(&ptime);
            std::stringstream                 s;
            s <<                              put_time(&local_time,"%Y %m %d %H %M %S");

            string addtime                    =s.str(),fh=" ";
            auto arr                          =split(addtime,fh);
            string years                      =to_string(atoi(arr[0].c_str())-1970);
            string moons                      =to_string(atoi(arr[1].c_str())-1);
            string suns                       =to_string(atoi(arr[2].c_str())-1);

            if (moons.length()==0) arr[1]     ="00";
            else if(moons.length()==1) arr[1] ="0"+moons;
            else if(moons.length()==2) arr[1] =moons;
            if (suns.length()==0) arr[2]      ="00";
            else if(suns.length()==1) arr[2]  ="0"+suns;
            else if(suns.length()==2) arr[2]  =suns;
            if (years.length()==0) arr[0]     ="0000";
            else if(years.length()==3) arr[0] ="0"+years;
            else if(years.length()==2) arr[0] ="00"+years;
            else if(years.length()==1) arr[0] ="000"+years;
            else arr[0]                       =years;

            json jt;
            for (int i = 0; i < 6; i++) jt["date"][i]   =arr[i];
            // string sql          ="UPDATE stime SET ctime='"+jt.dump()+"' where id=1;";
            // sqlite3_idu(sql);
            jt["status"]=1;

            file_put_contents(path,jt.dump()); 

            //滴嗒定时器开始
            sleep(1);
        }
    }    
}

int main(int argc, char const *argv[])
{
    std::string date="2025-01-29 00:00:00";
    timestart(date);
    callback();
    return 0;
}


